// CSE 142, Winter 2008, Marty Stepp // // This program draws a random 100x100 rectangle on a DrawingPanel with a // random positions and color. // The program demonstrates random numbers with graphics. import java.awt.*; // for Graphics, Point import java.util.*; // for Random public class RandomRectangles1 { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(500, 500); Graphics g = panel.getGraphics(); Random rand = new Random(); // choose a random location Point p = new Point(); p.x = rand.nextInt(400) + 1; p.y = rand.nextInt(400) + 1; // choose a random color int randomColor = rand.nextInt(3); // 0 through 2 if (randomColor == 0) { g.setColor(Color.RED); } else if (randomColor == 1) { g.setColor(Color.GREEN); } else { // 2 g.setColor(Color.BLUE); } g.fillRect(p.x, p.y, 100, 100); } }